package pannel.recipe;

import pannel.user.User;
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class CommentDialog extends JDialog {
    private Recipe recipe;
    private User currentUser;
    private CommentDao commentDao;
    private JPanel commentsPanel;
    private JTextArea newCommentArea;
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public CommentDialog(Window owner, Recipe recipe, User currentUser) {
        super(owner, "评论 - " + recipe.getName(), ModalityType.APPLICATION_MODAL);
        this.recipe = recipe;
        this.currentUser = currentUser;
        this.commentDao = new CommentDao();
        setupDialog();
    }

    private void setupDialog() {
        setLayout(new BorderLayout());
        setSize(600, 500);
        setLocationRelativeTo(getOwner());

        // 评论列表面板
        commentsPanel = new JPanel();
        commentsPanel.setLayout(new BoxLayout(commentsPanel, BoxLayout.Y_AXIS));
        JScrollPane scrollPane = new JScrollPane(commentsPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);

        // 发表评论面板
        JPanel inputPanel = new JPanel(new BorderLayout());
        newCommentArea = new JTextArea(3, 20);
        newCommentArea.setLineWrap(true);
        newCommentArea.setWrapStyleWord(true);
        JScrollPane commentScroll = new JScrollPane(newCommentArea);
        
        JButton submitButton = new JButton("发表评论");
        submitButton.addActionListener(e -> submitComment());

        inputPanel.add(commentScroll, BorderLayout.CENTER);
        inputPanel.add(submitButton, BorderLayout.EAST);
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        add(scrollPane, BorderLayout.CENTER);
        add(inputPanel, BorderLayout.SOUTH);

        refreshComments();
    }

    private void refreshComments() {
        commentsPanel.removeAll();
        try {
            List<Comment> comments = commentDao.getByRecipeId(recipe.getId());
            if (comments.isEmpty()) {
                JLabel emptyLabel = new JLabel("暂无评论", SwingConstants.CENTER);
                emptyLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                commentsPanel.add(emptyLabel);
            } else {
                for (Comment comment : comments) {
                    commentsPanel.add(createCommentPanel(comment));
                }
            }
            commentsPanel.revalidate();
            commentsPanel.repaint();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "加载评论失败：" + e.getMessage());
        }
    }

    private JPanel createCommentPanel(Comment comment) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY),
            BorderFactory.createEmptyBorder(10, 10, 10, 10)
        ));

        // 用户信息和时间
        JPanel headerPanel = new JPanel(new BorderLayout());
        JLabel userLabel = new JLabel(comment.getUsername());
        userLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        JLabel timeLabel = new JLabel(comment.getCreateTime().format(DATE_FORMATTER));
        timeLabel.setForeground(Color.GRAY);
        headerPanel.add(userLabel, BorderLayout.WEST);
        headerPanel.add(timeLabel, BorderLayout.EAST);

        // 评论内容
        JTextArea contentArea = new JTextArea(comment.getContent());
        contentArea.setEditable(false);
        contentArea.setWrapStyleWord(true);
        contentArea.setLineWrap(true);
        contentArea.setBackground(panel.getBackground());
        contentArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        contentArea.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

        panel.add(headerPanel, BorderLayout.NORTH);
        panel.add(contentArea, BorderLayout.CENTER);

        return panel;
    }

    private void submitComment() {
        String content = newCommentArea.getText().trim();
        if (content.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入评论内容");
            return;
        }

        try {
            Comment comment = Comment.builder()
                    .recipeId(recipe.getId())
                    .userId(currentUser.getId())
                    .content(content)
                    .build();

            if (commentDao.insert(comment)) {
                newCommentArea.setText("");
                refreshComments();
                JOptionPane.showMessageDialog(this, "评论成功！");
            } else {
                JOptionPane.showMessageDialog(this, "评论失败！");
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "评论失败：" + e.getMessage());
        }
    }
} 